package net.cellkyborg.server;

import net.cellkyborg.RSInstances.*;

public class PlayerHandler {

	public static final int maxPlayers = 2001;
	public RSPlayer players[] = new RSPlayer[maxPlayers];

    public PlayerHandler() {
    	for (int i = 0; i < maxPlayers; i++) players[i] = null;
    }
    
    public boolean updateRunning = false;
    
    public void process() {
    	for (int i = 1; i <maxPlayers; i++) {
    		if (players[i] != null && players[i].loaded) {
	    		players[i].preProcessing();
	    		if (players[i].destroy) {
	    			players[i].disconnect();
	    			players[i] = null;
	    		}
    		}
    	}
    	
    	for (int i = 1; i < maxPlayers; i++) {
    		if (players[i] != null && players[i].loaded) {
    			players[i].process();
    		}
    	}
    	for (int i = 1; i < maxPlayers; i++) {
    		if (players[i]!=null && players[i].loaded) {
    			players[i].postProcessing();
    		}
    	}

    }
    
    public void shutdown() {
    	
    }
    
    public void newPlayerClient(java.net.Socket s) {
    	int slot = -1;
    	for (int i = 1; i < maxPlayers; i++) {
    		if (players[i] == null) {
    			slot = i;
    			break;
    		}
    	}
    	if (slot == -1) {
    		try {
    			s.getOutputStream().write(7);
    			s.close();
    			return;
    		}
    		catch (java.io.IOException ioexception) {
    			return;
    		}	
    	}
    	players[slot] = new RSPlayer(s,slot);
    }
    
    public boolean isPlayerOn(String pName) {
    	for (int i = 1; i < maxPlayers; i++) {
    		if (players[i] != null && players[i].loaded) {
    			if (pName.equals(players[i].playerName)) 
    				return true;
    		}
    	}
    	return false;
    }
}